package com.utc.cards.table.jade.agent.gameAgent;

import jade.core.Agent;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.content.Context;
import android.content.Intent;

import com.utc.cards.Constants;
import com.utc.cards.model.HostModel;
import com.utc.cards.model.deck.Deck;
import com.utc.cards.model.game.InfoType;
import com.utc.cards.model.player.Hands;
import com.utc.cards.model.player.IPlayer;

// listen for players to send CARD
public class GameAgent extends Agent implements IGameAgent
{
    private static final long serialVersionUID = 1978219257952412130L;
    private static Logger log = LoggerFactory.getLogger(GameAgent.class);

    private HostModel model;
    private Context context;

    @Override
    protected void setup()
    {
        super.setup();
        Object[] args = getArguments();
        if (args != null && args.length > 0)
        {
            if (args[0] instanceof Context)
            {
                context = (Context) args[0];
            }
            else
            {
                log.error("Missing Context arg during agent setup");
            }
            if (args[1] instanceof HostModel)
            {
                model = (HostModel) args[1];
            }
            else
            {
                log.error("Missing HostModel arg during agent setup");
            }
        }

        // Add initial behaviours
        log.debug("add behaviour  GameBehaviour to GameAgent");
        addBehaviour(model.getGame().createGameBehaviour(this));

        // expose l'interface pour la rendre accessible par les activity
        registerO2AInterface(IGameAgent.class, this);
    }

    public HostModel getModel()
    {
        return model;
    }

    @Override
    public Context getContext()
    {
        return context;
    }

    // via DameDePiqueBehaviour
    @Override
    public void giveTurn(IPlayer player)
    {
        log.debug("add behaviour GameGiveTurnBehaviour to GameAgent");
        addBehaviour(new GameGiveTurnBehaviour(this, player));
    }

    // via DameDePiqueBehaviour, utilisé pour la distribution
    // remplacer toutes les cartes du joueurs
    @Override
    public void setCardsToPlayer(Map<String, Deck> hands, IPlayer player)
    {
        sendCardActionBehaviour(InfoType.SET_CARDS, hands, player);
    }

    // via DameDePiqueBehaviour, utilisé avant les échanges
    @Override
    public void addCardsToPlayer(Map<String, Deck> hands, IPlayer player)
    {
        sendCardActionBehaviour(InfoType.ADD_CARDS, hands, player);
    }

    // via DameDePiqueBehaviour, utilisé après chaque coup joué et validé
    @Override
    public void removeCardsToPlayer(Map<String, Deck> hands, IPlayer player)
    {
        sendCardActionBehaviour(InfoType.REMOVE_CARDS, hands, player);
    }

    // pas utilisé
    @Override
    public void clearCardsToPlayer(Map<String, Deck> hands, IPlayer player)
    {
        sendCardActionBehaviour(InfoType.CLEAR_CARDS, hands, player);
    }

    private void sendCardActionBehaviour(InfoType infoType, Map<String, Deck> cardsMap, IPlayer player)
    {
        log.debug("add behaviour GameSendCardBehaviour to GameAgent");
        addBehaviour(new GameSendCardBehaviour(this, infoType, new Hands(cardsMap), player));
    }

    @Override
    public void launchGameView()
    {
        Intent intent = new Intent(Constants.SHOW_HOST_GAME);
        log.debug("Sending INTENT " + intent.getAction());
        context.sendBroadcast(intent);
    }
}
